package com.yuan.gather.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.Properties;

/**
 * 通用工具类
 *
 * @author zhaoff
 */
@Slf4j
public class CommUtils {

    /**
     * 数据存放路径
     */
    private static String dataDir = null;

    /**
     * 资管配置文件内容（/etc/yuan/asset.conf）
     */
    private static Properties platformConfig = null;

    /**
     * 配置文件位置
     */
    //public static final String CONFIG_FILE = "/etc/yuan/asset.conf";
    public static final String CONFIG_FILE = "C:\\Users\\FoleyZhao\\Desktop\\asset.conf";

    /**
     * 读取平台配置文件
     *
     * @return 配置属性内容
     */
    public static synchronized Properties getAssetConfigurations() {
        if (platformConfig != null && !platformConfig.isEmpty()) {
            return platformConfig;
        }
        platformConfig = new Properties();
        File configFile = new File(CONFIG_FILE);
        if (configFile.exists()) {
            Reader reader = null;
            try {
                reader = new BufferedReader(new FileReader(configFile));
                platformConfig.load(reader);
            } catch (Exception e) {
                log.warn("Failed to read config: " + CONFIG_FILE, e);
            } finally {
                IOUtils.closeQuietly(reader);
            }
        }
        return platformConfig;
    }

    /**
     * 返回数据存放路径。
     *
     * @return 数据存放路径。
     */
    public static String getDataDir() {
        if (dataDir == null) {
            dataDir = System.getProperty("asset.datadir");
            if (dataDir == null) {
                if (new File(CONFIG_FILE).exists()) {
                    try {
                        dataDir = getAssetConfigurations().getProperty("asset.datadir");
                    } catch (Exception e) {
                        log.warn("Failed to read config file: " + CONFIG_FILE, e);
                    }
                }
            }
            if (dataDir == null) {
                File dir = new File(System.getProperty("user.home"), "asset_data");
                if (!dir.exists()) {
                    if (!dir.mkdirs()) {
                        log.warn("Failed to mkdirs config dir: " + CONFIG_FILE);
                    }
                }
                dataDir = dir.getAbsolutePath();
            }
        }
        return dataDir;
    }

    /**
     * 采集任务的最大并发数
     *
     * @return 采集任务的最大并发数
     */
    public static int getGatherTaskNum() {
        return getNumInternal("business.gather.taskNum", 4);
    }

    /**
     * 获取数字类型的属性
     *
     * @param propStr    属性的键
     * @param defaultNum 默认的值
     * @return 指定属性的值
     */
    private static int getNumInternal(String propStr, int defaultNum) {
        int ret = defaultNum;
        try {
            String str = getAssetConfigurations().getProperty(propStr);
            ret = Integer.parseInt(str);
            if (ret <= 0) {
                ret = defaultNum;
            }
        } catch (Exception e) {
            // ignore
        }
        return ret;
    }

}
